#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTextBrowser>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->TBdisplay->setAlignment(Qt::AlignRight);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_Btn0_released()
{
    inputString +="0";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn00_released()
{
    inputString+=".";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn1_released()
{
    inputString+="1";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn2_released()
{
    inputString+="2";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn3_released()
{
    inputString +="3";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn4_released()
{
    inputString +="4";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn5_released()
{
    inputString +="5";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn6_released()
{
    inputString +="6";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn7_released()
{
    inputString +="7";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn8_released()
{
    inputString +="8";
    brushScreen();
    showReult();
}

void MainWindow::on_Btn9_released()
{
    inputString +="9";
    brushScreen();
    showReult();
}

void MainWindow::on_BtnAdd_released()
{
    inputString +="+";
    brushScreen();
    showReult();
}

void MainWindow::on_BtnSubstraction_released()
{
    inputString +="-";
    brushScreen();
    showReult();
}

void MainWindow::on_BtnMultiplication_released()
{
    inputString +="×";
    brushScreen();
    showReult();
}

void MainWindow::on_BtnDivision_released()
{
    inputString +="÷";
    brushScreen();
    showReult();
}

void MainWindow::on_BtnClearAll_released()
{
    inputString .clear();
    ui->TBdisplay->clear();
    brushScreen();
}

void MainWindow::on_BtnEqual_released()
{
    brushScreen();
    calculator1();
    showReult();

}

void MainWindow::on_BtnClear_released()
{
    inputString.chop(1);
    brushScreen();
    showReult();
}

void MainWindow::on_leftBucket_released()
{
    inputString +="(";
    brushScreen();
    showReult();
}
void MainWindow::on_rightBucket_released()
{
    inputString +=")";
    brushScreen();
    showReult();
}
void MainWindow::on_helpPush_released()
{
    brushScreen();
    ui->TBdisplay->setAlignment(Qt::AlignHCenter);
    ui->TBdisplay->textCursor().insertText("\nHuamonarchCalculator version 1.0");
}

//刷新屏幕
void MainWindow::brushScreen()
{
    ui->TBdisplay->clear();
    ui->TBdisplay->setAlignment(Qt::AlignRight);
    ui->TBdisplay->textCursor().insertText(inputString);
    ui->TBdisplay->textCursor().insertText("\n");
}
//显示结果
void MainWindow::showReult()
{
    ui->TBdisplay->setAlignment(Qt::AlignRight);
    ui->TBdisplay->textCursor().insertText("= ");
    ui->TBdisplay->textCursor().insertText(outputString);
}
string MainWindow::inputString2Calinput()
{
    QString s = inputString;
    s = s.replace(QRegExp("×"),"*");
    s = s.replace(QRegExp("÷"),"/");
    return s.toStdString();
}

//计算器1
void MainWindow::calculator1()
{
    string s = inputString2Calinput();
    auto idx1=s.find('+');
    auto idx2=s.find('-');
    auto idx3=s.find('*');
    auto idx4=s.find('/');
    if(idx1== string::npos&&idx2== string::npos&&idx3== string::npos&&idx4== string::npos)
    {
        outputString = inputString;
    }
    else
    {
        Caldata cal1(inputString2Calinput());
        if(cal1.islegalinput()) outputString = QString::fromStdString(cal1.getResult());
    }
}






